package com.service.bank.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.client.common.config.IdWorker;
import com.client.model.pojo.bank.*;

import com.client.model.pojo.bank.Record;
import com.client.model.pojo.user.ArUser;
import com.client.model.system.ArBankcardTypeVo;
import com.client.model.system.ElectronicUserVo;
import com.client.model.system.ArElectronicBankCardVo;
import com.service.bank.mapper.ArBankcardMapper;
import com.service.bank.mapper.ArCardtypeMapper;
import com.service.bank.service.ArElectronicService;
import com.service.bank.mapper.ArElectronicMapper;
import com.service.bank.service.RecordService;
import com.service.bank.utlis.SaltUtil;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.util.*;

import java.util.List;

/**
* @author liuwenju
* @description 针对表【ar_electronic】的数据库操作Service实现
* @createDate 2024-04-01 20:55:27
*/
@Service
public class ArElectronicServiceImpl extends ServiceImpl<ArElectronicMapper, ArElectronic>
    implements ArElectronicService{
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ArElectronicMapper arElectronicMapper;
    @Autowired
    private ArBankcardMapper arBankcardMapper;
    @Autowired
    private ArCardtypeMapper arCardtypeMapper;
    @Autowired
    private RecordService recordService;
    @Override
    public boolean findByNameAndMobile(ElectronicUserVo electronicUserVo,String token) {


        String userinfo = this.redisTemplate.opsForValue().get("user:login"+token);
        System.err.println(userinfo);
        ArUser sysUser = JSON.parseObject(userinfo, ArUser.class);
        System.err.println("sysUser："+sysUser);

        if(electronicUserVo.getRealName().equals(sysUser.getRealName()) && electronicUserVo.getMobile().equals(sysUser.getMobile())){
            Random random=new Random();
            Integer uid = this.arElectronicMapper.selectUid(sysUser.getUid());
            if(uid==1){
                return false;
            }else {
                this.arElectronicMapper.save(sysUser.getUid(),random.nextInt(1000000000)+"");
                return true;
            }
        }else {
            return false;
        }


    }

    @Override
    public boolean setPayPassword(ElectronicUserVo electronicUserVo,String token) {
        String userinfo = this.redisTemplate.opsForValue().get("user:login"+token);
        ArUser sysUser = JSON.parseObject(userinfo, ArUser.class);

        System.err.println("salt----"+sysUser.getSalt());
        String pass = DigestUtils.md5DigestAsHex((sysUser.getSalt()).getBytes());
        System.err.println(pass);
        this.arElectronicMapper.setPayPassword(pass,sysUser.getUid());
        return true;
    }

    @Override
    public boolean zhuanzhang(ArElectronicBankCardVo arElectronicBankCardVo) {
        Record record=new Record();
        IdWorker idWorker=new IdWorker();
        record.setBalance(arElectronicBankCardVo.getBalance());
        record.setRid(idWorker.nextId());
        record.setToCard(arElectronicBankCardVo.getBankCards());
        record.setMainCard(arElectronicBankCardVo.getBankCard());
        record.setOperatorStatus(0);
        this.recordService.save(record);
        this.arBankcardMapper.deductMoney(arElectronicBankCardVo.getBalance(),arElectronicBankCardVo.getBankCard());
        Long uid = this.arBankcardMapper.findUid(arElectronicBankCardVo.getBankCards());
        this.arBankcardMapper.collectMoney(arElectronicBankCardVo.getBalance(),uid);

        return true;
    }

    @Override
    public boolean findName( ElectronicUserVo electronicUserVo,String token) {

        String userinfo = this.redisTemplate.opsForValue().get("user:login"+token);
        ArUser sysUser = JSON.parseObject(userinfo, ArUser.class);

        System.err.println("electronicUserVo.getMobile()"+electronicUserVo.getMobile());
        if(electronicUserVo.getMobile().equals(sysUser.getMobile())){
            return true;
        }
        return false;
    }

    @Override
    public boolean findBankCard(ArElectronicBankCardVo arElectronicBankCardVo) {
        System.err.println(arElectronicBankCardVo);
        ArBankcard bankCard = this.arBankcardMapper.findBankCard("张三");
        System.err.println(bankCard);
        return true;
    }

    @Override
    public List<ArBankcardTypeVo> findBankCards(ArElectronicBankCardVo arElectronicBankCardVo,String token) {
        String userinfo = this.redisTemplate.opsForValue().get("user:login"+token);
        System.err.println("userinfo："+userinfo);
        ArUser sysUser = JSON.parseObject(userinfo, ArUser.class);
        String bankCard = arElectronicBankCardVo.getBankCards();
        List<ArBankcardTypeVo> bankCards = this.arBankcardMapper.findBankCards(bankCard,sysUser.getRealName());

        return bankCards;
    }

    @Override
    public ArElectronic getEid(String token) {
        String userInfo = redisTemplate.opsForValue().get("user:login" + token);
        ArUser sysUser = JSON.parseObject(userInfo, ArUser.class);
        Long uid = sysUser.getUid();
        return this.arElectronicMapper.selectEid(uid);
    }

    @Override
    public boolean findPassword(ArBankCardPassword arBankCardPassword) {

        String pass = DigestUtils.md5DigestAsHex((arBankCardPassword.getPayPassword() + "qjbz").getBytes());

        Integer password = this.arBankcardMapper.findPassword(pass, arBankCardPassword.getBankCard());

        System.err.println("password：="+password);
        System.err.println("pass：="+pass);
        if(password==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean findNames(String relName,String token) {
        String userinfo = this.redisTemplate.opsForValue().get("user:login"+token);
        ArUser sysUser = JSON.parseObject(userinfo, ArUser.class);
        System.out.println(sysUser.getRealName());
        if(relName.equals(sysUser.getRealName())){
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean yzBankCards(ArElectronicBankCardVo arElectronicBankCardVo) {
        ArrayList<String> list = this.arBankcardMapper.yzBankCards(arElectronicBankCardVo.getRealName());

        for (String list1 : list){
            if(list1.equals(arElectronicBankCardVo.getBankCards())){
                return true;
            }
        }
        return false;

    }

    @Override
    public String yzType(ArElectronicBankCardVo arElectronicBankCardVo) {
//        ArCardtype arCardtype = this.arBankcardMapper.yzType(arElectronicBankCardVo.getTname());
//        System.err.println("arCardtype："+arCardtype);
//        System.err.println("arCardtypetid："+arCardtype.getTid());
//        Long tid1 = arCardtype.getTid();
//        Long tid = this.arElectronicMapper.findTid(arElectronicBankCardVo.getBankCards());
//        System.err.println("tid："+tid);
//        if(tid1.equals(tid)){
//            return true;
//        }else {
//            return false;
//        }
        Integer integer = this.arBankcardMapper.yzTid(arElectronicBankCardVo.getBankCards());
        String tnames = this.arCardtypeMapper.findTnames(integer);
        return tnames;
    }

    @Override
    public String findBalance(String bankCard) {
        String s = this.arBankcardMapper.selectBalance(bankCard);
        System.out.println("s："+s);
        return s;

    }

    @Override
    public Integer findTids(String bankCard) {
        Integer tids = this.arElectronicMapper.findTids(bankCard);
        System.out.println("tids："+tids);
        return tids;
    }

    @Override
    public Money yzBalances(Money money) {
        BigDecimal chooseBalance =money.getChooseBalance();
        BigDecimal inputBalance = money.getInputBalance();
        System.out.println("chooseBalance-----"+chooseBalance);
        System.out.println("inputBalance-----"+inputBalance);

        if(inputBalance.compareTo(chooseBalance) == -1){
            if(inputBalance.compareTo(BigDecimal.valueOf(5000))==-1){
                money.setMessage("不需要手续费！");
                return money;
            }else if(inputBalance.compareTo(BigDecimal.valueOf(5000))==1 && inputBalance.compareTo(BigDecimal.valueOf(5000))==0 && inputBalance.compareTo(BigDecimal.valueOf(10000))==-1){
                money.setInputBalance(money.getInputBalance().add(BigDecimal.valueOf(5)));
                money.setMessage("手续费：5元");
                return money;
            }else if(inputBalance.compareTo(BigDecimal.valueOf(10000))==1 && inputBalance.compareTo(BigDecimal.valueOf(10000))==0 && inputBalance.compareTo(BigDecimal.valueOf(50000))==-1){
                money.setInputBalance(money.getInputBalance().add(BigDecimal.valueOf(7.5)));
                money.setMessage("手续费：7.5元");
                return money;
            }else if(inputBalance.compareTo(BigDecimal.valueOf(50000))==1){
                money.setInputBalance (money.getInputBalance().add(inputBalance.multiply(BigDecimal.valueOf(0.015))));
                money.setMessage("手续费："+inputBalance.multiply(BigDecimal.valueOf(0.015))+"元");
                return money;
            }
        }
        else if(inputBalance.compareTo(chooseBalance) == 0){
            if(inputBalance.compareTo(BigDecimal.valueOf(5000))==-1){
                money.setMessage("不需要手续费！");
                return money;
            }else if(inputBalance.compareTo(BigDecimal.valueOf(5000))==1 && inputBalance.compareTo(BigDecimal.valueOf(5000))==0 && inputBalance.compareTo(BigDecimal.valueOf(10000))==-1){
                money.setInputBalance(money.getInputBalance().subtract(BigDecimal.valueOf(5)));
                money.setMessage("手续费：5元");
                return money;
            }else if(inputBalance.compareTo(BigDecimal.valueOf(10000))==1 && inputBalance.compareTo(BigDecimal.valueOf(10000))==0 && inputBalance.compareTo(BigDecimal.valueOf(50000))==-1){
                money.setInputBalance(money.getInputBalance().subtract(BigDecimal.valueOf(7.5)));
                money.setMessage("手续费：7.5元");
                return money;
            }else if(inputBalance.compareTo(BigDecimal.valueOf(50000))==1){
                money.setInputBalance (money.getInputBalance().subtract(inputBalance.multiply(BigDecimal.valueOf(0.015))));
                money.setMessage("手续费："+inputBalance.multiply(BigDecimal.valueOf(0.015))+"元");
                return money;
            }
        }
        return money;
    }

    @Override
    public ArElectronic getElectronicByEid(Long uid) {
        return this.arElectronicMapper.selectEid(uid);
    }


}




